﻿using System;
using System.Linq;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using System.Collections.Generic;

namespace Wicresoft.EKA.ITSM.BusinessImpl
{
    public class SMGXJPlanObject : Base,ISMGXJPlanObject
    {
        protected override void Dispose(bool disposing)
        {
        }

        public IEnumerable<SMGXJPlanObjectModel> GetList(string Qual)
        {
            var proxy = this.GetARProxy<SMGXJPlanObjectModel>();
            var list = proxy.GetListEntry(Qual, new System.Collections.Generic.List<uint>()
                {
                    SMGXJPlanObjectModel.RequestID_ID,
                    SMGXJPlanObjectModel.cChr_Type_ID,
                    SMGXJPlanObjectModel.cChr_DeviceName_ID,
                    SMGXJPlanObjectModel.cChr_Classification_ID,
                    SMGXJPlanObjectModel.cChr_Location_ID,
                    SMGXJPlanObjectModel.cChr_RelatedTicketID_ID,
                    SMGXJPlanObjectModel.cChr_IP_ID,
                    SMGXJPlanObjectModel.cChr_Hostname_ID,
                    SMGXJPlanObjectModel.cChr_Use_ID,
                    SMGXJPlanObjectModel.cChr_SQLVersion_ID,
                    SMGXJPlanObjectModel.cChr_DiskLable_ID,
                    SMGXJPlanObjectModel.cChr_Indicators_ID,
                    SMGXJPlanObjectModel.cChr_OrderID_ID,
                    SMGXJPlanObjectModel.cChr_IndicatorIDs_ID,
                    SMGXJPlanObjectModel.cChr_Description_ID,
                    SMGXJPlanObjectModel.cChr_Jifang_ID,
                    SMGXJPlanObjectModel.cChr_louceng_ID,
                    SMGXJPlanObjectModel.cChr_jigui_ID
                }).ToList();
            return list;
        }

        public string Create(SMGXJPlanObjectModel model)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanObjectModel>();
                //这里每次创建的时候都有一个filter去pushSMGXJPlanObjectTemplateModel的数据
                proxy.CreateEntry(model);
            }
            catch (Exception e)
            {
                return "Fail :" + e.Message;
            }
            return "Success";
        }

        public void Createlist(List<SMGXJPlanObjectModel> modellist)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanObjectModel>();
                //这里每次创建的时候都有一个filter去pushSMGXJPlanObjectTemplateModel的数据
                proxy.CreateListEntry(modellist);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public string Modify(SMGXJPlanObjectModel model)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanTicketRuleRelatedModel>();
                var list = proxy.GetListEntry("'cChr_TicketID'=\"" + model.cChr_RelatedTicketID + "\" AND 'cChr_MarkAsDelete' = \"No\" AND 'cChr_ObjectID' = \"" + model.cChr_DeviceName + "\"", new System.Collections.Generic.List<uint>()
                {
                    SMGXJPlanTicketRuleRelatedModel.cChr_RuleName_ID,
                }).ToList();
                string listL="";
                if (list.Count() != 0)
                {
                    for (var i = 0; i < list.Count(); i++) listL = listL + ";"+list[i].cChr_RuleName;
                    model.cChr_Indicators = listL.TrimStart(';');
                }
                else
                {
                    model.cChr_Indicators = " ";
                }
                //var proxy2 = this.GetARProxy<SMGXJPlanObjectModel>();
                //proxy2.SetEntryByQuery(model, "'Request ID' = \"" +model.RequestID+"\"" , BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyFirst);
                return model.cChr_Indicators.ToString();
            }
            catch (Exception e)
            {
                return "Fail :" + e.Message;
            }
        }

        public string Delete(string TicketID)
        {
            try
            {
                var proxy2 = this.GetARProxy<SMGXJPlanObjectModel>();
                proxy2.DeleteEntryByQuery("'cChr_RelatedTicketID' = \"" + TicketID + "\"");
                return "Success";
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public string ModifyALL(SMGXJPlanObjectModel model,string Qual)
        {
            try
            {
                var proxy2 = this.GetARProxy<SMGXJPlanObjectModel>();
                proxy2.SetEntryByQuery(model, Qual, BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyAll);
                return "Success";
            }
            catch (Exception e)
            {
                return "Fail :" + e.Message;
            }
        }
    }
}
